#!/bin/bash
yum -y install epel-release
yum -y install wget make
#set parameters
#for python
pver=`curl https://www.python.org/|grep Latest:|awk -F '[><]' '{print $5}'|cut -d ' ' -f 2`
pyver=${pver%.*}
pydir=/usr/local/python$pyver
#for openssl
# sslv=1.1.1g
# openssl '1.x series' 
sslpackage=`curl https://www.openssl.org/source/|grep openssl-1|awk -F '[<>]' '{print $5}'`
ssldir=/usr/local/ssl

#install necessary packages
yum -y install zlib-devel libffi-devel
#install other extensions
yum -y install bzip2-devel ncurses-devel sqlite-devel gdbm-devel xz-devel tk-devel readline-devel

#install openssl
cd ~
#make sure empty files
rm -rf openssl* $ssldir
wget --no-check-certificate https://ftp.openssl.org/source/$sslpackage
tar -zxvf $sslpackage
cd ${sslpackage%.tar*}
./config --prefix=$ssldir shared zlib
make && make install
#modify links
if [ ! -f /usr/bin/openssl.old ];then
    mv -f /usr/bin/openssl /usr/bin/openssl.old
fi
if [ ! -f /usr/include/openssl.old ];then
    mv -f /usr/include/openssl /usr/include/openssl.old
fi
ln -s $ssldir/bin/openssl /usr/bin/openssl 
ln -s $ssldir/include/openssl /usr/include/openssl
#add to share
echo "$ssldir/lib" > /etc/ld.so.conf.d/openssl.conf
ldconfig
#add so-file to system
ln -s /usr/local/ssl/lib/libssl.so /usr/lib64/libssl.so
ln -s /usr/local/ssl/lib/libcrypto.so /usr/lib64/libcrypto.so

#install python
cd ~
#make sure empty files
rm -rf Python* $pydir /usr/local/python*
wget --no-check-certificate https://www.python.org/ftp/python/$pver/Python-$pver.tar.xz
tar -xvJf Python-$pver.tar.xz
cd Python-$pver

#use gcc-8.x.x
yum install -y centos-release-scl scl-utils-build
yum install -y devtoolset-8-toolchain
#scl enable devtoolset-8 bash
source /opt/rh/devtoolset-8/enable

#continue configure
./configure --with-openssl=$ssldir --prefix=$pydir --enable-optimizations
make && make install

#create links
rm -rf /usr/local/bin/python3 /usr/local/bin/pip3
ln -s $pydir/bin/python$pyver /usr/local/bin/python3
ln -s $pydir/bin/pip3 /usr/local/bin/pip3

#set source orgin
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

#update pip
pip3 install pip -U

#install wheel
pip3 install wheel